// 
//  Copyright 2011  comphilip
// 
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
// 
//        http://www.apache.org/licenses/LICENSE-2.0
// 
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using StringPair = System.Collections.Generic.KeyValuePair<string, string>;

namespace Dict4Engkoo.Engkoo {
	internal class Define {
		private JToken _token0, _token1;
		
		public Define(JToken Token0, JToken Token1){ //Token is DEF.SENS
			_token0 = Token0;
			_token1 = Token1;
		}
		
		public string Character {
			get {
				JToken pos = _token0["$POS"];
				return pos != null ? Uri.UnescapeDataString(pos.Value<string>()) : string.Empty;
			}
		}
		
		public IEnumerable<Explanation> Explanations {
			get {
				JToken sen0 = _token0["SEN"];
				JToken sen1 = _token1 != null ? _token1["SEN"] : null;
				int length = GetLength(sen0);
				int length2 = GetLength(sen1);
				if (length < length2)
					length2 = length;
				
				for(int i=0; i < length; ++i){
					yield return GetExplanation(GetItem(sen0, i), GetItem(sen1, i));	
				}
			}
		}
		
		private static JToken GetItem(JToken Token, int Index){
			if (Token == null)
				return null;
			
			JArray array = Token as JArray;
			if (array != null){
				if (array.Count > Index && Index >=0)
					return array[Index];
			}else if (Index == 0)
				return Token;
			
			return null;
		}
		
		private static int GetLength(JToken Token){
			int length = 0;
			if (Token != null)
				length = 1;
			JArray array = Token as JArray;
			if (array != null)
				length = array.Count;
			
			return length;
		}
		
		private static Explanation GetExplanation(JToken Token0, JToken Token1){
			Explanation result = new Explanation();
			if (Token0 != null){
				JToken d = Token0["D"];
				if (d != null)
					result.Explanation1 = Uri.UnescapeDataString(d["$"].Value<string>());
				
				JToken sts = Token0["STS"];
				if (sts != null){
					JToken st = sts["ST"];
					if (st != null){
						JToken e = st["E"];
						JToken c = st["C"];
						result.Sentence2 = e != null ? Uri.UnescapeDataString(e["$"].Value<string>()) : string.Empty;
						result.Sentence1 = c != null ? Uri.UnescapeDataString(c["$"].Value<string>()) : string.Empty;
					}
				}
			}
			
			if (Token1 != null){
				JToken d = Token1["D"];
				if (d != null)
					result.Explanation2 = Uri.UnescapeDataString(d["$"].Value<string>());
			}
			
			return result;
		}
		
		internal class Explanation {
			public Explanation(){
				Explanation1 = string.Empty;
				Explanation2 = string.Empty;
				Sentence1 = string.Empty;
				Sentence2 = string.Empty;
			}
			public string Explanation1 {get; set;}
			public string Explanation2 {get; set;}
			public string Sentence1 {get;  set;}
			public string Sentence2 {get;  set;}
		}
	}
}

